<template>
    <div>
        <div class="filing-box">
            <h3>企业备案修改</h3>
            <el-form style="max-width:500px;margin: 0 auto;" status-icon ref='companyNameForm' class='login-form'>
                <el-form-item prop='companyName' label='企业全称'>
                    <el-input type='text' v-model='companyName'></el-input>
                </el-form-item>
                <el-form-item prop='companyCode' label='企业社会统一信用代码 '>
                    <el-input type='text' v-model='companyCode'></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type='primary' @click="updateCompanyName" :loading='loading'
                               style='width:100%'>提交
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import {changeCompanyName, getUserInfo} from '@/api/register';

    export default {
        data() {
            return {
                companyName: '',
                loading: false,
                companyCode: '',
                loading: false,
            }
        },
        created() {
            if (localStorage.getItem('UserInfo')) {
                this.companyName = JSON.parse(localStorage.getItem('UserInfo')).companyNameCN;
            }
            getUserInfo().then(res => {
                if (res.code == 0) {
                    this.companyName = res.data.companyInfo.companyHsName;
                    this.companyCode = res.data.companyInfo.companyCode;
                    this.loading = false;
                }
            })
        },
        methods: {
            updateCompanyName() {
                if (!this.companyName) {
                    this.$notify.error({
                        title: '错误',
                        message: '请输入企业全称'
                    });
                    return;
                } else {
                    this.loading = true;
                    changeCompanyName(this.companyName, this.companyCode).then(res => {
                        if (res.code == 0) {
                            this.$message({
                                message: '企业名称更改成功',
                                type: 'success'
                            })
                            getUserInfo().then(res => {
                                if (res.code == 0) {
                                    localStorage.setItem('UserInfo', JSON.stringify(res.data.userInfo));
                                    this.companyName = res.data.userInfo.companyNameCN;
                                    this.companyCode = res.data.companyInfo.companyCode;
                                    this.$router.go(0)
                                    this.loading = false;
                                }
                            })
                        } else {
                            this.$message.error(res.msg)
                        }
                    }).catch(err => {
                        this.$message.error(err)
                    })
                }
            },
        }
    }
</script>

<style scoped lang="scss">
    .filing-box {
        background: #fff;
        width: 500px;
        margin: auto;
        padding: 20px 30px;
        border-radius: 6px;

        h3 {
            text-align: center;
        }
    }
</style>